function sleep(time) {
    return new Promise((resolve, reject)=>{
        setTimeout(function () {
            console.log(time)
            resolve('OK --- ')  // 返回值 'OK --- '
        },time)
    })
};

async function fun() {
    try {
        console.log("0, Start")
        let s1 = await sleep(500)   //await等待的虽然是promise对象，但不必写.then(..)，直接可以得到返回值。
        console.log("1, Waiting ...",s1)

        let s2 = sleep(1000)
        console.log("2, Waiting ...",s2)

        let s3 = await sleep(1000)
        console.log("3, End",s3)

        for (let i = 0; i < 5; i++) {
            console.log(`当前是第${i}次等待..`);
            await sleep(1000);      // 循环多个await
            console.log(new Date, i);
        }

    }catch(err) {       // 直接用标准的try catch语法捕捉错误 `error`
        console.log(err)
    }
};

fun()


// async / await的重要应用

const asy = function(x, time) {
    return new Promise((resolve, reject) =>{
        setTimeout(()=>{
            resolve(x)
        }, time)
    })
}

const add = async function() {
    const a = await asy(3, 5000)
    console.log(a)
    const b = await asy(4, 10000)
    console.log(b)
    const c =  await asy(5, 15000)
    console.log(a,b,c)
    const d = a + b +c
    console.log(d)
}

add();

// 5秒后输出3  又10秒后输出4 又15秒后输出5  然后立刻输出3,4,5，然后输出12